<!--suppress ALL -->
<div class="layui-fluid layui-anim website-anim" id="website-topic" lay-title="课题管理"
     xmlns:th="http://www.w3.org/1999/xhtml">
    <div class="layui-row website-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body website-table-full">
                    <form class="layui-form layui-table-form" lay-filter="topic-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md10">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">课题名称</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="topicName" autocomplete="off" class="layui-input"
                                                   placeholder="请输入标题">
                                            <input type="hidden" name="planId" autocomplete="off" class="layui-input"
                                                   data-th-value="${planId}"/>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">学科</label>
                                        <div class="layui-input-inline">
                                            <select name="subjectTypeCode">
                                                <option value="">请选择</option>
                                                <option
                                                        th:each="item : ${subjectType}"
                                                        th:value="${item.code}" th:text="${item.name}">
                                                </option>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-blue-plain table-action"
                                     id="query">
                                    查询
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-green-plain table-action"
                                     id="reset">
                                    重置
                                </div>
                                <div th:classappend="${isGenerateCode} ? 'website-hide' : '' "
                                     class="layui-btn layui-btn-sm layui-btn-primary website-button-blue-plain table-action"
                                     id="addTopic">
                                    添加
                                </div>
                                <a th:classappend="${isGenerateCode} ? 'website-hide' : '' " class="" title="生成编码"
                                   style="margin-left: 6px;">
                                    <div class="layui-btn layui-btn-sm layui-btn-primary website-button-blue-plain table-action"
                                         id="creatCode">
                                        生成指南序号
                                    </div>
                                </a>
                            </div>
                        </div>
                    </form>
                    <table lay-filter="topicTable" ></table>
                </div>
            </div>
        </div>
    </div>
</div>
<!--表格中字典的显示-->
<script type="text/html" id="subject_type">
    {{#
    var dictList=layui.dict.getByCodes(d.subjectTypeCodes);
    }}
    <span>
    {{# layui.each(dictList,function(index,item) { }}
    {{ item.name }}&nbsp;
    {{#  }); }}
    </span>
</script>
<!--表格中字典的显示-->
<script type="text/html" id="topic_classify_code">
    {{#
    var dict=layui.dict.getByCode(d.topicClassifyCode);
    }}
    {{#  if(dict){ }}
    <span>{{ dict.name }}</span>
    {{#  } }}
</script>

<!--表格中的操作栏-->
<script type="text/html" id="topic-option">
    <span shiro:lacksPermission="topic:list,topic:update,topic:delete">
        <span class="layui-badge-dot website-bg-orange"></span> 无权限
    </span>
    <a lay-event="detail" shiro:hasPermission="topic:list" class="website-green">详情</a>
    <a lay-event="edit" shiro:hasPermission="notice:update" class="website-blue"
       th:classappend="${isGenerateCode} ? 'website-hide' : '' ">修改</a>
    <a lay-event="del" class="website-red" shiro:hasPermission="notice:delete"
       th:classappend="${isGenerateCode} ? 'website-hide' : '' ">删除</a>
</script>

<script data-th-inline="javascript" type="text/javascript">
    layui.use(['jquery', 'form', 'table', 'website', 'dict', 'laydate'], function () {
        var $ = layui.jquery,
            website = layui.website,
            form = layui.form,
            table = layui.table,
            dict = layui.dict,
            dropdown = layui.dropdown,
            laydate = layui.laydate,
            $view = $('#website-topic'),
            $addTopic = $view.find('#addTopic'),
            $export = $view.find('#export'),
            planId = [[${planId}]],
            year = [[${year}]],
            $query = $view.find('#query'),
            $reset = $view.find('#reset'),
            $creatCode = $view.find('#creatCode'),
            $toChanges = $view.find('#toChanges'),
            $searchForm = $view.find('form'),
            sortObject = {field: 'createTime', type: null},
            tableIns,
            createTimeFrom,
            createTimeTo;
        form.render();
        initTable();

        /**
         * table操作栏的按钮监听事件
         **/
        table.on('tool(topicTable)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'detail') {
                website.modal.view('课题详情', 'biz/topic/topicDetail/' + data.topicId, {
                    area: $(window).width() <= 750 ? '95%' : '50%'
                });
            }
            if (layEvent === 'del') {
                website.modal.confirm('删除课题', '确定删除该课题？', function () {
                    deleteTopic(data.topicId);
                });
            }
            if (layEvent === 'edit') {
                debugger
                website.modal.open('修改课题', 'biz/topic/topicUpdate/' + data.topicId, {
                    area: $(window).width() <= 750 ? '90%' : '50%',
                    offset: '30px',
                    btn: ['提交', '取消'],
                    yes: function (index, layero) {
                        $('#topic-update').find('#submit').trigger('click');
                    },
                    btn2: function () {
                        layer.closeAll();
                    }
                });
            }
        });

        /**
         * 表格的排序点击
         **/
        table.on('sort(topicTable)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });

        $query.on('click', function () {
            var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            tableIns.reload({where: params, page: {curr: 1}});
        });

        $addTopic.on('click', function () {
            website.modal.open('新增课题', 'biz/topic/topicAdd/' + planId, {
                btn: ['提交', '重置'],
                area: $(window).width() <= 750 ? '95%' : '50%',
                offset: '30px',
                yes: function (index, layero) {
                    $('#topic-add').find('#submit').trigger('click');
                },
                btn2: function () {
                    $('#topic-add').find('#reset').trigger('click');
                    return false;
                }
            });
        });

        $export.on('click', function () {
            var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            debugger
            // website.download(ctx + 'expert/export', params, '中国法学会2017年度部级法学研究课题建议征集汇总表.xlsx');
        });

        $reset.on('click', function () {
            $searchForm[0].reset();
            sortObject.type = 'null';
            createTimeTo = null;
            createTimeFrom = null;
            tableIns.reload({where: getQueryParams(), page: {curr: 1}, initSort: sortObject});
        });

        $creatCode.on('click', function () {
            website.modal.confirm('生成编码', '生成编码后不允许进行任何修改，是否要生成？', function () {
                website.post(ctx + 'biz/topic/createCode/' + planId + '/' + year, null, function () {
                    website.alert.success('生成课题编码成功！');
                    window.location.reload();
                });
            });
        });

        /**
         * 初始化表格
         */
        function initTable() {
            tableIns = website.table.init({
                elem: $view.find('table'),
                id: 'topicTable',
                url: ctx + 'biz/topic/list',
                where: {year: year},
                cols: [[
                    /*{type: 'checkbox'},*/
                     { type: 'numbers', title: '序号', },
                    {field: 'topicCode', title: '课题序号'},
                    {field: 'topicName', title: '课题名称', minWidth: 100},
                    {title: '学科分类', templet: '#subject_type'},
                    {title: '课题类型', templet: '#topic_classify_code'},
                    {title: '操作', toolbar: '#topic-option', minWidth: 140}
                ]]
            });
        }

        /**
         * 获取查询条件
         * @returns {{}}
         */
        function getQueryParams() {
            var params = $searchForm.serializeJson();
            return params;
        }

        /**
         * 删除方法
         * @param noticeIds
         */
        function deleteTopic(noticeId) {
            website.post(ctx + 'biz/topic/delete/' + noticeId, null, function () {
                website.alert.success('删除课题成功！');
                $query.click();
            });
        }

    })
</script>